Arithmetic Operations:
- Write a PHP script to calculate the area of a rectangle using arithmetic operators. Display the
result. (area of a rectangle: $area = $length * $width; )
- Implement a script that converts currency from one denomination to another using arithmetic
operators. Display the converted amount. (Convert currency: $converted_amount = $amount *
$conversion_rate;)
A. Solution
$length = 2;
$width = 3;
$area = $length * $width
echo "Convert currency: $area";
Output
Area of rectangle: 6
B. Solution
$amount = 50;
$conversion_rate = 56;
$converted_amount = $amount * $conversion_rate;
echo "Convert currency: $converted_amount";
Output
Convert currency: 2800
Assignment Operators:
- Track the progress of a fundraising campaign by updating the donation total dynamically
using combined assignment operators. Display the updated total. (Track fundraising:
$total_donation += $new_donation;)
- Implement a voting system where candidate votes are incremented based on user input.
Display the final vote count for each candidate. (Voting system: $candidate_votes +=
$user_vote;)
A. Solution
$total_donation = 500;
$new_donation = 1000;
$total_donation += $new_donation
echo "Track fund raising: $total_donation";
Output
Track fund raising: 1500B. Solution
$candidate_votes = 100;
$user_vote = 50;
$candidate_votes += $user_vote;
echo "Voting System: $candidate_votes";
Output
Voting System: 150
Comparison Operators:
- Compare the performance of two athletes in a race and determine the winner using
comparison operators. Display the result. (Compare athlete times: Use '<' to compare times.)
- Evaluate the efficiency of two algorithms based on their execution times using comparison
operators. Display the more efficient algorithm. (Evaluate algorithm efficiency: Use '<' to
compare times.)
A. Solution
$athletes1= 50;
$athletes2 = 100;
$winner = $athletes1 < $athletes2
echo "the winner is: $winner";
Output
the winner is: 1B. Solution
$algorithms1 = 100;
$algorithms2 = 50;
$winner = $algorithms1 < $algorithms2 ;
echo "Evaluate algorithm efficiency: $winner";
Output
faster algorithm efficiency: 1
Increment/Decrement Operations:
- Calculate the factorial of a number using postfix increment/decrement operations. Display the
factorial. (Calculate factorial: Use a loop to multiply numbers.)
- Simulate the movement of a vehicle along a track using prefix increment/decrement
operations. Display the final position of the vehicle. (Simulate vehicle movement: Use += for
forward movement.)
A. Solution
$num = 10;
$factorial = 1;
for ($i = 1; $i <= $n; $i++) {
$factorial *= $i;
}
echo "Calculate factorial of $num: $factorial";
Output
Calculate factorial of 10 : 3628800B. Solution
$position = 10;
$distance = 10;
$position += $distance;
echo "vehicle movement:e: $position";
Output
Vehicle movement: 20
Logical Operators:
- Determine eligibility for a discount based on purchase amount and customer loyalty using
logical operators. Display whether the customer is eligible for a discount. (Determine discount
eligibility: Use && to check conditions.)
- Design a decision-making system for a chatbot to respond to user queries using logical
operators. Display the appropriate response based on the query. (Chatbot decision-making: Use ?
: for simple conditions.)
A. Solution
$purchaseAmount = 100;
$loyaltyPoints = 50;
$minPurchaseAmount = 50;
$minLoyaltyPoints = 40;
$isEligibleForDiscount = ($purchaseAmount >= $minPurchaseAmount) && ($loyaltyPoints >= $minLoyaltyPoints);
echo "Determine discount
eligibility: $isEligibleForDiscount";
Output
Discount
eligibility: EligibleB. Solution
$user_query = "Good Morning";
$response = ($user_query == "weather") ? "Good Morning " : "Bad Morning";
echo "Chatbot response: $response";
Output
Chatbot desicion-making: Good Morning! :)